package com.boliq.lol.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.boliq.demo.R;
import com.boliq.lol.bean.NewsArticleBean;

import java.util.ArrayList;
import java.util.List;


public class NewsArticleAdapter  extends RecyclerView.Adapter <NewsArticleAdapter.NViewHolder> {


    private List<NewsArticleBean> newsData;
    public NewsArticleAdapter() {
        newsData = new ArrayList<>();
    }
    //构造器，接受数据集
    @NonNull
    @Override
    public NViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_article, parent, false);
        return new NViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(@NonNull NViewHolder holder, int position) {
        NewsArticleBean bean = newsData.get(position);
        holder.img.setImageResource(bean.getImg());
        holder.title.setText(bean.getTitle());
        holder.tag.setText(bean.getTag());
        holder.user.setText(bean.getUser());
        holder.number.setText(bean.getNumber());
    }



    @Override
    public int getItemCount() {
        return newsData.size();
    }

    public void addData(NewsArticleBean bean) {
        newsData.add(bean);
        notifyItemInserted(newsData.size());
    }
    public void addData(List<NewsArticleBean> data) {
        int lastSize = newsData.size();
        newsData.addAll(data);
        notifyItemRangeInserted(lastSize, data.size());
    }

    public class NViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;
        private TextView title;
        private TextView tag;
        private TextView user;
        private TextView number;

        public NViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img_cover);
            title = itemView.findViewById(R.id.tv_article_title);
            tag = itemView.findViewById(R.id.tv_tag);
            user = itemView.findViewById(R.id.tv_article_user);
            number = itemView.findViewById(R.id.tv_view_number);
        }
    }
}
